package com.visikard.asynctask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.visikard.application.UIMessage;
import com.visikard.business.GiftKardBUS;
import com.visikard.response.BaseResponse;
import com.visikard.response.RingUpResponse;
import com.visikard.response.RingUpResponse.RingUpResultEnum;

public class RingUpTask extends BaseTask {

	public static final String KEY_AMOUNT_REDEEM = "key_amount_redeemed";
	public static final String KEY_PREVIOUS_BALANCE = "key_previous_balance";
	public static final String KEY_PUNCH_RECORDED = "key_punch_recorded";

	public static final int POS_MERCHANT_ID = 0;
	public static final int POS_STAFF_ID = 1;
	public static final int POS_UID = 2;
	public static final int POS_TRANSACTION_ID = 3;
	public static final int POS_STORE_ID = 4;
	public static final int POS_AMOUNT_REDEEMED = 5;
	public static final int POS_PREVIOUS_BALANCE = 6;
	public static final int POS_IS_GIFTKARD = 7;
	public static final int POS_REDEEM_ADDRESS = 8;

	public RingUpTask(Handler handler) {
		super(handler);
	}

	public RingUpTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doBackgroundWork(Object... params) {
		// Get params
		long merchantId = (Long) params[POS_MERCHANT_ID];
		long staffId = (Long) params[POS_STAFF_ID];
		long uid = (Long) params[POS_UID];
		long transactionId = (Long) params[POS_TRANSACTION_ID];
		long storeId = (Long) params[POS_STORE_ID];
		String amountRedeemed = (String) params[POS_AMOUNT_REDEEMED];
		String previousBalance = (String) params[POS_PREVIOUS_BALANCE];
		int isGiftKard = (Integer) params[POS_IS_GIFTKARD];
		String redeemAddress = (String) params[POS_REDEEM_ADDRESS];

		// call service
		GiftKardBUS busObj = new GiftKardBUS();
		return busObj.ringUp(merchantId, staffId, uid, transactionId, storeId, amountRedeemed, previousBalance,
				isGiftKard, redeemAddress);
	}

	@Override
	protected boolean handleResult(Object result) {
		RingUpResponse response = (RingUpResponse) result;
		if (response != null) {
			switch (response.getErrorCode()) {
				case BaseResponse.SUCCESS:
					if (response.getResult() == RingUpResultEnum.SUCCESS) {
						Bundle data = new Bundle();
						data.putDouble(KEY_AMOUNT_REDEEM, response.getAmountRedeemed());
						data.putDouble(KEY_PREVIOUS_BALANCE, response.getPreviousBalance());
						data.putInt(KEY_PUNCH_RECORDED, response.getPunchRecorded());
						Message msg = new Message();
						msg.what = UIMessage.MSG_RINGUP_SUCCESS;
						msg.setData(data);
						mUIHandler.sendMessage(msg);
					}
					else if (response.getResult() == RingUpResultEnum.EXPIRED) {
						mUIHandler.sendEmptyMessage(UIMessage.MSG_RINGUP_EXPIRED);
					}
					else {
						mUIHandler.sendEmptyMessage(UIMessage.MSG_RINGUP_FAILED);
					}
					return true;
				case BaseResponse.NETWORK_ERROR:
					mUIHandler.sendEmptyMessage(UIMessage.MSG_RINGUP_FAILED);
					return false;
				default: // unknown error
					mUIHandler.sendEmptyMessage(UIMessage.MSG_RINGUP_FAILED);
					return false;
			}
		}
		return false;
	}

}
